<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/16
 * Time: 15:39
 */
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\request;

/**
 *
 * 提现页面
 */
class Deposit extends Base{
    //提现页面
    public function deposit(){

        $tx = Db::name('config')->where(['key'=>'txtime'])->find()['value'];
       // var_dump(111);die;
        $s = explode('-',$tx);
        $this->assign('s',$s);
        $da = time();

        if($da > strtotime(date("Y-m-d $s[0]")) && $da < strtotime(date("Y-m-d $s[1]"))){
        }else if($da > strtotime(date("Y-m-d $s[2]")) && $da < strtotime(date("Y-m-d $s[3]"))){
        }else{
            $cc = 1;
            $this->assign('cc',$cc);
        }
       //var_dump(Session::get('uid'));die;
        //用户
       $data=Db::name('users')->where('id='.Session::GET('uid'))->find();
      //  var_dump();die;
        if($data['RNA']==1)
        {   //是否添加银行卡或支付宝
            //是否设密码
            if($data['paymentPassword']==null){
                //var_dump($data);
                $this->error('请先设置支付密码','setpassword/index');
            }

            $ban=Db::name('user_bank')->where('uid='.Session::get('uid').' and  state=0')->select();

            if($ban!=null){
                //提现比例
                // $column=Db::name('column')->find();
                $config = Db::name('config')->where(['key'=>'rate'])->find()['value'];
                //var_dump($config);
                $this->assign('config',$config);
                //用户提现信息
                $this->assign('data',$data);
                //银行卡或支付宝提现选择
                $this->assign('ban',$ban);
                //累计提现金额
                $total=Db::name('user_withdrawcash')->where('uid='.Session::get('uid').' and  state=3')->sum('amount');
                //var_dump($total);
                if($total==null){
                    $total=0;
                }


                $this->assign('total',$total);
                if(request()->isPost()){
//                    return "<script>alert('提现时间截止为1月25日下午4点整。2月11日恢复正常');window.location.href='".url('home/Deposit/deposit')."'</script>";
//                    die;
                    if(date('w')==6 || date('w')==0){
                        $this->error('非工作日不能提现','Deposit/deposit');
                    }
                    if(isset($cc)==1){
                        $this->error("$s[0]~$s[1]/$s[2]~$s[3]为提现时间","Deposit/deposit");
                    }

                    if(!is_numeric(input('amount'))) return "<script>alert('金额格式错误');window.location.href='".url('home/Deposit/deposit')."'</script>";
                    Session::set('codes','111');
                    if(Session::get('codes')!=111){
                        $this->error("请勿重复提交","Deposit/deposit");
                    }
                    Session::get('codes',mt_rand(1000,9999));
                    $configs = Db::name('config')->where(['key'=>'最小提现金额'])->find()['value'];
                    if(input('amount') < $configs) return "<script>alert('提现金额必须大于{$configs}元');window.location.href='".url('home/Deposit/deposit')."'</script>";
                    $list=[
                        'uid'=>Session::get('uid'),
                        'amount'=>input('amount'),
                        'bankId'=>input('bankId'),
                    ];
                    $list['poundage']=$config*input('amount');
                    $list['addTime']=date('Y-m-d H:i:s');
                    /*
                       $list['uid']=Session::get('uid');
                       $list['amount']=$_POST['amount'];
                       $list['poundage']=$column['column']*$_POST['amount'];
                       $list['bankId']=$_POST['bankId'];
                       $list['password']=$_POST['password'];
                     */
                    $pwd=md5($data['usercode'].input('paymentPassword'));
                    // var_dump($pwd);
                    if($pwd==$data['paymentPassword']){
                        //余额是否充足
                        $balance=$data['balance']-$list['amount']-$list['poundage'];
                        //提现金额要大于0
                        if($list['amount']>0){
                            if($balance >=0 ){
                             //用户是否异常
                                //提现未到账的
                                 $str=Db::name('user_withdrawcash')->where('uid',Session::GET('uid'))->where('state','in','1,2')->sum('amount');
		                         $strr=Db::name('user_withdrawcash')->where('uid',Session::GET('uid'))->where('state','in','1,2')->sum('poundage');
                                //资金日志
                                $zij=Db::name('cashlog')->where('uid='.Session::GET('uid'))->order('id desc')->limit(0,1)->find();
                                //用户余额加提现未到账
                                $zamount=$data['balance']+$str+$strr;
                           /*     var_dump($zamount);
                                var_dump($zij['balance']);
                                if($zamount==$zij['balance']){
                                	echo 111;
                                }
                                die;*/
                                //数据是否异常
                                if($zij['balance']!=$zamount){
                                  $list['remakes']='用户余额和资金日志异常';
                                }
                                $arr=Db::table('qfll_config')->where('key','tx')->find();
                                //开启事务
                                Db::startTrans();
                               try{

                                    //还剩下的金额
                                    $ste_1=Db::table('qfll_users')->where('id', Session::get('uid'))->update(['balance' => $balance]);
                                    $ste=Db::name('user_withdrawcash')->insertGetId($list);
                                    //事务提交
                                    Db::commit();
                                    //短信提示
/*
                                    $mobile=$arr['value'];
                                    $tplParam1=$data['name'].'--TX';
                                    $tpCode="SMS_135807275";
                                    $tplParam=$list['amount'];
                                    //var_dump($tplParam);
                                    $message = sendMsgs($mobile,$tpCode,$tplParam1,$tplParam);*/
                                   // var_dump($message);die;

                                   return  "<script>alert('申请提现成功');window.location.href='".url('home/Deposit/deposit')."'</script>";

                                } catch (\Exception $e)
                                {
                                    //回滚事务
                                    Db::rollback();
                                    return  "<script>alert('申请提现失败');window.location.href='".url('home/Deposit/deposit')."'</script>";
                                }

                            }else{
                                //提现余额不足
                                return  "<script>alert('提现余额不足');window.location.href='".url('home/Deposit/Deposit')."'</script>";
                            }
                        }else{
                            //提现余额大于0
                            return  "<script>alert('提现余额要大于0');window.location.href='".url('home/Deposit/Deposit')."'</script>";
                        }

                    }else{
                        return  "<script>alert('密码错误');window.location.href='".url('home/Deposit/Deposit')."'</script>";

                    }

                }


            }else{
                return  "<script>alert('请先填写银行卡或支付宝');window.location.href='".url('home/bankcards/index')."'</script>";
            }

        }else
        {
            return  "<script>alert('请先填写实名认证');window.location.href='".url('home/certification/index')."'</script>";
        }


        return $this->fetch('Deposit/deposit');
    }
}
